Option Strict On
Option Explicit On

Imports System.Collections.Generic

Public Class GestorPlanProtocolo

    Private id_pro As Integer

    Public Sub setPlanProtocolo(ByVal objPlanProtocolo As CapaDeNegocio.PlanProtocolo)

        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader

        objDataReader = objDataAccess.getDataReader("SELECT MAX(ID_PROTOCOLO) FROM LABT_PLAN_PROTOCOLO")
        Dim s_id As String
        Dim v_id As Integer

        objDataReader.Read()
        If objDataReader Is Nothing Then
            v_id = 1
            objDataAccess.CloseConnection()
        Else
            s_id = objDataReader.Item(0).ToString
            v_id = CType(s_id, Integer)
            v_id = v_id + 1
            objDataAccess.CloseConnection()
        End If
        id_pro = v_id
        Dim objMiDataAccess As laboratorio_Datos.NVDataAccess = New laboratorio_Datos.NVDataAccess

        objMiDataAccess.setDataAccess(" INSERT INTO LABT_PLAN_PROTOCOLO (ID_PROTOCOLO, CODIGO, OBJETIVO, PATOLOGIA, FECHA_APROBACION, FECHA_INICIO, FECHA_ELEV_INFORMES, CANTIDAD_WEEKS, CANTIDAD_PACIENTES, NUM_MAX_PACIENTES, NUM_MIN_PACIENTES, INTERVALO_CONTENCION, MEDICAMENTO) VALUES ('" & CStr(v_id) & "', ' " & objPlanProtocolo.codigo & "', '" & objPlanProtocolo.objetivo & "', '" & objPlanProtocolo.patologia & "','" & objPlanProtocolo.fechaAprobacion & "','" & objPlanProtocolo.FechaInicio & "', '" & objPlanProtocolo.fechaElevacionInformes & "', " & objPlanProtocolo.cantidadWeeks & ", " & objPlanProtocolo.cantidadPaciente & ", " & objPlanProtocolo.numMaxPacientes & ", " & objPlanProtocolo.numMinPacientes & ", " & objPlanProtocolo.intervaloContencion & ", '" & objPlanProtocolo.medicamento & "')")
        objMiDataAccess.CloseConnection()

    End Sub

    Public Sub setEtapas(ByVal objEtapa As CapaDeNegocio.Etapa)

        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader

        objDataReader = objDataAccess.getDataReader("SELECT MAX(ID_ETAPA) FROM LABT_ETAPA")
        Dim s_id As String
        Dim v_id As Integer

        objDataReader.Read()
        If objDataReader Is Nothing Then
            v_id = 1
            objDataAccess.CloseConnection()
        Else
            s_id = objDataReader.Item(0).ToString
            If s_id.CompareTo("") = 0 Then
                s_id = "0"
            End If
            v_id = CType(s_id, Integer)
            v_id = v_id + 1
            objDataAccess.CloseConnection()
        End If
        Dim objMiDataAccess As laboratorio_Datos.NVDataAccess = New laboratorio_Datos.NVDataAccess

        objMiDataAccess.setDataAccess("INSERT INTO LABT_ETAPA (ID_ETAPA, ID_PROTOCOLO, DESCRIPCION, FECHA_INICIO, FECHA_FIN, ORDEN_EJECUCION) VALUES ('" & CStr(v_id) & "', '" & CStr(id_pro) & "', '" & objEtapa.descripcion & "', '" & objEtapa.fechaInicio & "', '" & objEtapa.fechaFin & "', " & objEtapa.ordenEjecucion & ")")
        objMiDataAccess.CloseConnection()
    End Sub

    Public Sub setWeeks(ByVal objWeek As Laboratorio_Negocio.CapaDeNegocio.Week, ByVal id_etapa As String)
        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader

        objDataReader = objDataAccess.getDataReader("SELECT MAX(ID_WEEK) FROM LABT_WEEK")
        Dim s_id As String
        Dim v_id As Integer

        objDataReader.Read()
        If objDataReader Is Nothing Then
            v_id = 1
            objDataAccess.CloseConnection()
        Else
            s_id = objDataReader.Item(0).ToString
            If s_id.CompareTo("") = 0 Then
                s_id = "0"
            End If
            v_id = CType(s_id, Integer)
            v_id = v_id + 1
            objDataAccess.CloseConnection()
        End If
        Dim objMiDataAccess As laboratorio_Datos.NVDataAccess = New laboratorio_Datos.NVDataAccess

        objMiDataAccess.setDataAccess("INSERT INTO LABT_WEEK (ID_WEEK, NUMERO, FECHA_INICIO, FECHA_FIN, ID_ETAPA) VALUES ('" & CStr(v_id) & "', " & objWeek.numero & ", '" & objWeek.fechaInicio & "', '" & objWeek.fechaFin & "', '" & id_etapa & "')")
        objMiDataAccess.CloseConnection()

    End Sub
    Public Function getProtocolo(ByVal id_protocolo As String) As Laboratorio_Negocio.CapaDeNegocio.PlanProtocolo

        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader
        Dim objProtocolo As Laboratorio_Negocio.CapaDeNegocio.PlanProtocolo

        objDataReader = objDataAccess.getDataReader("SELECT * FROM LABT_PLAN_PROTOCOLO WHERE id_protocolo='" & id_protocolo & "'")

        While objDataReader.Read
            objProtocolo = New Laboratorio_Negocio.CapaDeNegocio.PlanProtocolo
            With objProtocolo
                .idProtocolo = objDataReader!id_protocolo.ToString
                .codigo = objDataReader!CODIGO.ToString
                .patologia = objDataReader!PATOLOGIA.ToString
            End With
        End While

        objDataAccess.CloseConnection()
        Return objProtocolo

    End Function
    Public Function getEtapa(ByVal id_etapa As String, ByVal strIdProtocolo As String) As Laboratorio_Negocio.CapaDeNegocio.Etapa

        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader
        Dim objEtapa As Laboratorio_Negocio.CapaDeNegocio.Etapa

        objDataReader = objDataAccess.getDataReader("SELECT * FROM LABT_ETAPA WHERE upper(id_etapa)='" & id_etapa & "'")

        While objDataReader.Read
            objEtapa = New Laboratorio_Negocio.CapaDeNegocio.Etapa
            With objEtapa
                .idetapa = objDataReader!ID_ETAPA.ToString
                .ordenEjecucion = CInt(objDataReader!ORDEN_EJECUCION.ToString)
                .descripcion = objDataReader!DESCRIPCION.ToString
            End With
        End While

        objDataAccess.CloseConnection()
        Return objEtapa

    End Function

    Public Function getProtocoloCodigo(ByVal codigo As String) As Laboratorio_Negocio.CapaDeNegocio.PlanProtocolo

        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader
        Dim objProtocolo As Laboratorio_Negocio.CapaDeNegocio.PlanProtocolo

        objDataReader = objDataAccess.getDataReader("SELECT * FROM LABT_PLAN_PROTOCOLO WHERE upper(codigo)='" & codigo.ToUpper & "'")

        While objDataReader.Read
            objProtocolo = New Laboratorio_Negocio.CapaDeNegocio.PlanProtocolo
            With objProtocolo
                .codigo = objDataReader!CODIGO.ToString
                .patologia = objDataReader!PATOLOGIA.ToString
            End With
        End While

        objDataAccess.CloseConnection()
        Return objProtocolo

    End Function

    Public Function getEtapaOrden(ByVal strOrden As String) As Laboratorio_Negocio.CapaDeNegocio.Etapa

        Dim objDataAccess As laboratorio_Datos.DataAccess = New laboratorio_Datos.DataAccess
        Dim objDataReader As OleDb.OleDbDataReader
        Dim objEtapa As Laboratorio_Negocio.CapaDeNegocio.Etapa

        objDataReader = objDataAccess.getDataReader("SELECT * FROM LABT_ETAPA WHERE orden_ejecucion='" & strOrden & "'")

        While objDataReader.Read
            objEtapa = New Laboratorio_Negocio.CapaDeNegocio.Etapa
            With objEtapa
                .idetapa = objDataReader!ID_ETAPA.ToString
                .ordenEjecucion = CInt(objDataReader!ORDEN_EJECUCION.ToString)
                .descripcion = objDataReader!DESCRIPCION.ToString
            End With
        End While

        objDataAccess.CloseConnection()
        Return objEtapa
    End Function

End Class
